package com.lst.selfaiagent.app;

import com.lst.selfaiagent.advisors.MyLoggerAdvisor;
import com.lst.selfaiagent.chatMemory.FileBasedChatMemory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class FileStorageLoveApp {

    private static final Logger log = LoggerFactory.getLogger(LoveApp.class);
    private final ChatClient chatClient;

    private static final String SYSTEM_PROMPT = "扮演深耕恋爱心理领域的专家。开场向用户表明身份，告知用户可倾诉恋爱难题。" +
            "围绕单身、恋爱、已婚三种状态提问：单身状态询问社交圈拓展及追求心仪对象的困扰；" +
            "恋爱状态询问沟通、习惯差异引发的矛盾；已婚状态询问家庭责任与亲属关系处理的问题。" +
            "引导用户详述事情经过、对方反应及自身想法，以便给出专属解决方案。";

    public FileStorageLoveApp(ChatModel ollamaModel) {
        // 初始化基于文件的对话记忆
        String fileDir = System.getProperty("user.dir") + "/chat-memory";
        ChatMemory chatMemory = new FileBasedChatMemory(fileDir);
        chatClient = ChatClient.builder(ollamaModel)
                .defaultSystem(SYSTEM_PROMPT)
                .defaultAdvisors(
                       MessageChatMemoryAdvisor.builder(chatMemory).build()
                        , new MyLoggerAdvisor()
                )
                .build();
    }

    // map结构化输出
    public Map<String, Object> doChat(String message, String chatId) {

Map<String, Object> result =
        chatClient.prompt()
        .user(u -> u.text("作为恋爱心理专家，请针对用户的问题提供3条高情商的回复建议。要求：1. 体现理解和共情；2. 给出具体可行的建议；3. 语言温和且有建设性。用户问题：{userQuestion}")
                .param("userQuestion", message))
        .call()
        .entity(new ParameterizedTypeReference<Map<String, Object>>() {});
    return result;

    }


}
